package com.lfg.leetcode.leetcode100;


public class No135candy {

    public static void main(String[] args) {
        System.out.println(candy(new int[]{1, 2, 3, 5, 4, 3, 2, 1, 4, 3, 2, 1, 3, 2, 1, 1, 2, 3, 4}));
    }

    public static int candy(int[] ratings) {
        if (ratings.length == 0) {
            return 0;
        }
        if (ratings.length == 1) {
            return 1;
        }
        int[] ret = new int[ratings.length];
        for (int i = 0; i < ratings.length; i++) {
            ret[i] = 1;
        }

        for (int i = 1; i < ratings.length; i++) {
            if (ratings[i] > ratings[i - 1] && ret[i] <= ret[i - 1] + 1) {
                ret[i] = ret[i - 1] + 1;
            }
        }
        for (int i = ratings.length - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1] && ret[i] <= ret[i + 1] + 1) {
                ret[i] = ret[i + 1] + 1;
            }
        }

        int total = 0;
        for (int i : ret) {
            total += i;
        }

        return total;
    }
}
